package conceptmaniax.hotelapp.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import conceptmaniax.hotelapp.activities.R;

public class SimpleListAdapter extends BaseAdapter {
	
    private List<Simple> simpleList;
	private Context context;
    
    public SimpleListAdapter(final Context context) {
        super();
        this.context = context;
        this.simpleList = new ArrayList<Simple>();
    }
    
    public void addItem(final Simple item) {
    	simpleList.add(item);
        notifyDataSetChanged();
    }
    
    @Override
    public boolean isEnabled(int position) {
        Simple simple = simpleList.get(position); 
		if (simple.getCode() == null) {
			if (position == 0) 
				return false;
		}
			return true;
    }

    @Override
    public int getCount() {
        return simpleList.size();
    }

    @Override
    public Object getItem(final int position) {
        return simpleList.get(position);
    }

    @Override
    public long getItemId(final int position) {
        return position;
    }
    
    @Override
    public int getItemViewType(int position) {
        return (position  == 0) ? 1 : 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public View getView(final int position, View view, final ViewGroup viewGroup) {   
        Simple simple = simpleList.get(position); 
        ViewHolder holder = null;
        int type = getItemViewType(position);
    	final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	if (view == null) {
	    	holder = new ViewHolder();
		    switch(type) {
			    case 0:
				      view = inflater.inflate(R.layout.callout_row, null);
				      holder.textView = (TextView)view.findViewById(R.id.menu_item_name);
				      break;
			    case 1:
			    	if (simple.getCode() == null) {
					      view = inflater.inflate(R.layout.basic_view_row, null);
					      holder.textView = (TextView)view.findViewById(R.id.basic_text);
			    	} else {
					      view = inflater.inflate(R.layout.callout_row, null);
					      holder.textView = (TextView)view.findViewById(R.id.menu_item_name);
			    	}
				      break;
		    } 
		      view.setTag(holder);
	    } else {
	    		holder = (ViewHolder)view.getTag();
       }
    	holder.textView.setText(simple.getName() );
    	return view;    
    }
    
    public static class ViewHolder {
        public TextView textView;
    }
}